<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic%7CRoboto+Slab:300,300italic,400,400italic,700,700italic%7CNoto+Serif+SC:300,300italic,400,400italic,700,700italic%7CAmita:300,300italic,400,400italic,700,700italic%7CMontserrat:300,300italic,400,400italic,700,700italic%7CPT+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wang-chen1119.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Pisces","darkmode":false,"version":"8.13.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/myblog/js/config.js"></script>

    <meta name="description" content="volatileJMM(Java内存模型)了解volatile关键字之前我们首先要知道一个概念就是JMM，我们都听说过JVM层面定义的内存模型，而JMM是Java内存模型，定义了程序中各个变量的访问方式。  总结一下就是">
<meta property="og:type" content="blog">
<meta property="og:title" content="volatile关键字">
<meta property="og:url" content="http://wang-chen1119.gitee.io/myblog/2022/10/29/volatile%E5%85%B3%E9%94%AE%E5%AD%97/index.html">
<meta property="og:site_name" content="Yorn&#39;s Notes">
<meta property="og:description" content="volatileJMM(Java内存模型)了解volatile关键字之前我们首先要知道一个概念就是JMM，我们都听说过JVM层面定义的内存模型，而JMM是Java内存模型，定义了程序中各个变量的访问方式。  总结一下就是">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411095612516.png">
<meta property="og:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411153933011.png">
<meta property="og:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411173413343.png">
<meta property="og:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411173947341.png">
<meta property="og:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411180045099.png">
<meta property="og:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411174727184.png">
<meta property="article:published_time" content="2022-10-29T05:53:25.895Z">
<meta property="article:modified_time" content="2022-10-29T05:53:19.687Z">
<meta property="article:author" content="yorn">
<meta property="article:tag" content="并发">
<meta property="article:tag" content="volatile">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411095612516.png">


<link rel="canonical" href="http://wang-chen1119.gitee.io/myblog/2022/10/29/volatile%E5%85%B3%E9%94%AE%E5%AD%97/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://wang-chen1119.gitee.io/myblog/2022/10/29/volatile%E5%85%B3%E9%94%AE%E5%AD%97/","path":"2022/10/29/volatile关键字/","title":"volatile关键字"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>volatile关键字 | Yorn's Notes</title>
  






  <noscript>
    <link rel="stylesheet" href="/myblog/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Yorn's Notes</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-首页"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于"><a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-标签"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-分类"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#volatile"><span class="nav-number">1.</span> <span class="nav-text">volatile</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#JMM-Java%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B"><span class="nav-number">1.1.</span> <span class="nav-text">JMM(Java内存模型)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#volatile%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86"><span class="nav-number">1.2.</span> <span class="nav-text">volatile的实现原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#volatile%E5%86%99-x2F-%E8%AF%BB%E7%9A%84%E5%86%85%E5%AD%98%E8%AF%AD%E4%B9%89"><span class="nav-number">1.3.</span> <span class="nav-text">volatile写&#x2F;读的内存语义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B3%E4%BA%8Evolatile%E5%86%85%E5%AD%98%E8%AF%AD%E4%B9%89"><span class="nav-number">1.4.</span> <span class="nav-text">关于volatile内存语义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A6%96%E5%85%88%E4%BA%86%E8%A7%A3%E4%B8%80%E4%B8%8B%E5%86%85%E5%AD%98%E5%B1%8F%E9%9A%9C%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-number">1.4.1.</span> <span class="nav-text">首先了解一下内存屏障的概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0volatile%E7%9A%84%E5%86%85%E5%AD%98%E8%AF%AD%E4%B9%89"><span class="nav-number">1.4.2.</span> <span class="nav-text">实现volatile的内存语义</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#volatile%E4%BF%9D%E8%AF%81%E6%9C%89%E5%BA%8F%E6%80%A7"><span class="nav-number">1.5.</span> <span class="nav-text">volatile保证有序性</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yorn"
      src="/myblog/uploads/head.jpg">
  <p class="site-author-name" itemprop="name">yorn</p>
  <div class="site-description" itemprop="description">足够专注才能成功</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/myblog/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/myblog/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/myblog/tags/">
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wang-chen1119" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wang-chen1119" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:w1905989051@gmail.com" title="E-Mail → mailto:w1905989051@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://blog.csdn.net/qq_51088445" title="https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_51088445" rel="noopener" target="_blank">CSDN</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/wang-chen1119" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://wang-chen1119.gitee.io/myblog/2022/10/29/volatile%E5%85%B3%E9%94%AE%E5%AD%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/uploads/head.jpg">
      <meta itemprop="name" content="yorn">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Yorn's Notes">
      <meta itemprop="description" content="足够专注才能成功">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="volatile关键字 | Yorn's Notes">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          volatile关键字
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-10-29 13:53:25 / 修改时间：13:53:19" itemprop="dateCreated datePublished" datetime="2022-10-29T13:53:25+08:00">2022-10-29</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/%E5%B9%B6%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">并发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="volatile"><a href="#volatile" class="headerlink" title="volatile"></a>volatile</h1><h2 id="JMM-Java内存模型"><a href="#JMM-Java内存模型" class="headerlink" title="JMM(Java内存模型)"></a>JMM(Java内存模型)</h2><p>了解volatile关键字之前我们首先要知道一个概念就是JMM，我们都听说过JVM层面定义的内存模型，而JMM是Java内存模型，定义了程序中各个变量的访问方式。</p>
<p><img src="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411095612516.png" alt="image-20220411095612516"></p>
<p>总结一下就是</p>
<ul>
<li>所有变量的存在主内存中</li>
<li>线程读取时会将变量读取到自己的本地内存中</li>
<li>线程操作会对自己本地内存中的变量进行操作，不能直接去读写主内存</li>
</ul>
<p>设计到内存交互的时候可以这样看这张图</p>
<p><img src="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411153933011.png" alt="image-20220411153933011"></p>
<p>这些操作的作用分别为</p>
<blockquote>
<p>assign : 作用于工作内存的变量，把从一个执行引擎收到的值赋给工作内存的变量，每当虚拟机遇到一个给变量赋值的字节码指令时执行这个操作</p>
<p>store : 作用于工作内存的变量，把工作内存中的一个变量的值传送到主内存中，以便随后的write操作</p>
<p>write ：作用于主内存的变量，把store操作从工作内存中一个变量的值传送到主内存的变量中</p>
<p>read ：作用于主内存变量，把变量值从主内存传输到工作内存中</p>
<p>load ： 将read操作从主内存中得到的变量值放入工作内存的变量副本中</p>
<p>use ：作用于工作内存的变量，把工作内存中的一个变量值传递给执行引擎，每当虚拟机遇到一个需要使用变量的字节码指令将会执行这个操作</p>
</blockquote>
<h2 id="volatile的实现原理"><a href="#volatile的实现原理" class="headerlink" title="volatile的实现原理"></a>volatile的实现原理</h2><p>关于volatile的在汇编层面的实现时在对有volatile修饰得共享变量进行写操作是会多出第二行汇编代码，以<code>lock</code>为前缀，而以<code>lock</code>为前缀修饰得指令在多核处理器下会引发两件事</p>
<ol>
<li>将当前处理器缓存行的数据写回到系统内存</li>
<li>这个写回内存的操作会使其他CPU里缓存了该内存地址的数据无效</li>
</ol>
<h2 id="volatile写-x2F-读的内存语义"><a href="#volatile写-x2F-读的内存语义" class="headerlink" title="volatile写&#x2F;读的内存语义"></a>volatile写&#x2F;读的内存语义</h2><p>写： 当写一个volatile变量时，JMM会把该线程对应的本地内存变量中的共享变量刷新到主内存中</p>
<p>读：当读一个volatile变量时，JMM会把该线程对应的本地内存置为无效，接下来线程会从主内存中读取共享变量</p>
<p>用线程通信的角度看就是</p>
<p><img src="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411173413343.png" alt="image-20220411173413343"></p>
<h2 id="关于volatile内存语义"><a href="#关于volatile内存语义" class="headerlink" title="关于volatile内存语义"></a>关于volatile内存语义</h2><h3 id="首先了解一下内存屏障的概念"><a href="#首先了解一下内存屏障的概念" class="headerlink" title="首先了解一下内存屏障的概念"></a>首先了解一下内存屏障的概念</h3><p>当执行程序的时编译器和处理器常常会对指令进行重排序来提高性能。</p>
<p>一般会经历3种指令重排序</p>
<p><img src="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411173947341.png" alt="image-20220411173947341"></p>
<p>编译器优化的重排序 ： 编译器在不改变单线程程序语义的前提下，可以重新安排语句的执行顺序</p>
<p>指令级并行的重排序：现代处理器采用了指令级并行技术来将多条指令重叠排序</p>
<p>内存系统重排序 ： 由于处理器使用缓存和读&#x2F;写缓冲区，使得加载和存储操作看上去可能是在乱序执行</p>
<p>为了保证内存的可见性，我们编译器在生成指令序列的时候会在适当位置插入内存屏障，来防止特定类型的处理器重排序</p>
<p>JMM针对编译器重排序定义的volatile重排序规则表</p>
<p><img src="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411180045099.png" alt="image-20220411180045099"></p>
<p>内存屏障类型表</p>
<p><img src="https://image-1305623006.cos.ap-nanjing.myqcloud.com/https://image-1305623006.cos.ap-nanjing.myqcloud.comimage-20220411174727184.png" alt="image-20220411174727184"></p>
<h3 id="实现volatile的内存语义"><a href="#实现volatile的内存语义" class="headerlink" title="实现volatile的内存语义"></a>实现volatile的内存语义</h3><ul>
<li><p>在每个volatile写操作的前面插入一个StoreStore屏障。 </p>
</li>
<li><p>在每个volatile写操作的后面插入一个StoreLoad屏障。 </p>
</li>
<li><p>在每个volatile读操作的后面插入一个LoadLoad屏障。 </p>
</li>
<li><p>在每个volatile读操作的后面插入一个LoadStore屏障。</p>
</li>
</ul>
<h2 id="volatile保证有序性"><a href="#volatile保证有序性" class="headerlink" title="volatile保证有序性"></a>volatile保证有序性</h2><p>volatile的一个特点是可以保证有序性，比较重要的例子就是单例模式中双重锁校验机制中volatile关键字的使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Singleton2</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">static</span> Singleton2 singleton;</span><br><span class="line">    <span class="keyword">private</span> <span class="title function_">Singleton2</span><span class="params">()</span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span>  Singleton2 <span class="title function_">getSingleton</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">synchronized</span> (Singleton2.class)&#123;</span><br><span class="line">            <span class="keyword">if</span> (singleton == <span class="literal">null</span>) &#123;</span><br><span class="line">                singleton = <span class="keyword">new</span> <span class="title class_">Singleton2</span>();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> singleton;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>yorn
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://wang-chen1119.gitee.io/myblog/2022/10/29/volatile%E5%85%B3%E9%94%AE%E5%AD%97/" title="volatile关键字">http://wang-chen1119.gitee.io/myblog/2022/10/29/volatile关键字/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/%E5%B9%B6%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> 并发</a>
              <a href="/myblog/tags/volatile/" rel="tag"><i class="fa fa-tag"></i> volatile</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2022/10/29/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/" rel="prev" title="Java并发编程的艺术读书笔记">
                  <i class="fa fa-chevron-left"></i> Java并发编程的艺术读书笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2022/10/29/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="next" title="设计模式学习笔记">
                  设计模式学习笔记 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yorn</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/myblog/js/comments.js"></script><script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script><script src="/myblog/js/bookmark.js"></script>

  





  





<script src="/myblog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
